<?php

class Article_model extends CI_Model{
	private $table_name="article";

	public function getAll(){
		$query = $this->db->get($this->table_name);// 把数据库的东西拿出来
            return $query->result_array();//return给控制器
	}

	public function count_all(){
		return $this->db->count_all_results($this->table_name);
	}

	public function get_by_page($page,$per_page){//取得这两个参数
		$offset=($page-1)*$per_page;//设定开始的页面，从该数字下一个开始
		$query = $this->db->limit($per_page,$offset)->order_by('id', 'DESC')->get($this->table_name);// 把数据库的东西拿出来，哪一行哪一页
            return $query->result_array();//return给控制器
	}	
	
	public function getOne($id){
		$query = $this->db->where("id",$id)->get($this->table_name);//拿到视图界面的id，然后通过id进行单独的操作
        return $query->row();
	}

	public function getCatalogId($catalog){ //拿到视图界面的id。
		$query = $this->db->where("catalog_id",$catalog)->get($this->table_name);//通过id和catalog进行关联
        return $query->result_array();
	}

	public function view_once($id){
		$this->db->query("update article set `view`=`view`+1 where `id`=$id");
	}

	public function add_one($data){
		$data['create_time'] = time();//创建添加时间，
		$this->db->insert($this->table_name,$data);
	}

	public function edit_one($id,$data){
		$data['create_time'] = time();
		$this->db->where('id',$id)->update($this->table_name, $data);
	}

	public function del_one($id){
		$this->db->where('id',$id)->delete($this->table_name);
	}
	public function rec_one($id){
			$query=$this->db->where('id',$id)->get($this->table_name);
			$a=$query->row();
			$recommend=$a->recommend;       //取出所需要进行处理的值；
			$recommend=$recommend==1?0:1;
			$data = array('recommend' => $recommend, );
			$this->db->where('id',$id)->update($this->table_name,$data);
	}
}